extends CanvasLayer

const CHARACTERS = 'abcdefghijklmnopqrstuvwxyz'



@onready var h_box_container_1: HBoxContainer = %HBoxContainer1
@onready var h_box_container_2: HBoxContainer = %HBoxContainer2
@onready var h_box_container_3: HBoxContainer = %HBoxContainer3
@onready var option_info: MarginContainer = $OptionInfo
@onready var shopping_button: Button = $ShoppingButton
@onready var store: Panel = $Store
@onready var destroy_button: Button = %DestroyButton
@onready var quit_button: Button = %QuitButton

@onready var info_content: Label = %InfoContent
@onready var info_name: Label = %InfoName





var current_selected_spot=0

var items: = []
var items_map: Dictionary[int, EquipItem] = {}



var equip_scene: PackedScene = preload("res://ui/package/equip_button.tscn")
var pakage_block_scene: PackedScene = preload("res://ui/package/pacakge_block.tscn")

var current_select_equip:String


func _ready() -> void:
	GameEvents.buy_it.connect(_buy_it)
	GameEvents.equip_select.connect(_equip_select)
	shopping_button.pressed.connect(shopping_button_pressed)
	quit_button.pressed.connect(_quit_option_inf_button_pressed)
	destroy_button.pressed.connect(_destroy_equip)




func _process(delta: float) -> void:
	#set_current_selected_spot()
	pass


func reset_select_item():
	var current_item = -1
	var new_item_inventory = []
	
	
func refresh_container(container:Node):
	var children_button = container.get_children()
	for node in children_button:
		node.queue_free()
	
	
	
func _buy_it(item_id:String,new_id:String):
	
	#判断背包是否满了，还需判断是否有足够的金钱
	if items.size()>=6:
		return
	
	items.append(item_id)
	
	var asa = EquipManager.get_equip(item_id)
	items_map[items.size()-1]  = asa
	
	#回调，告诉商店中的商品已经买好了
	GameEvents.bought.emit(new_id)
	refresh_package()
	
	
	
func refresh_package():
	if items.size()==0:
		var sum := EquipItem.new()
		EquipManager.current.emit(sum)
	
	
	var line1 = true
	var line2 = true
	var line3 = true
	
	var equip_array :Array[EquipItem]= []

	for i in range(items.size()):
		var item = equip_scene.instantiate() as EquipButton
		item.new_id= str(i)
		item.item_id = items[i]
		equip_array.append(items_map.get(i))
		if i/2 == 0:
			if line1:
				refresh_container(h_box_container_1)
			h_box_container_1.add_child(item)
			
			if items.size()-i==1 and line1:
				h_box_container_1.add_child(pakage_block_scene.instantiate())
			
			if line1:
				line1 = false
			
		if i/2 == 1:
			if line2:
				refresh_container(h_box_container_2)
			h_box_container_2.add_child(item)
			
			if items.size()-i==1 and line2:
				h_box_container_2.add_child(pakage_block_scene.instantiate())
			
			if line2:
				line2 = false
			
		if i/2 == 2:
			if line3:
				refresh_container(h_box_container_3)
			h_box_container_3.add_child(item)
			if items.size()-i==1 and line3:
				h_box_container_3.add_child(pakage_block_scene.instantiate())
			
			if line3:
				line3 = false
		
		#总装备 属性加成 传输给玩家
		var sum := EquipManager.get_caculate(equip_array)
		EquipManager.current.emit(sum)
		
		
	
	
func _equip_select(item_id:String,new_id:String):
	option_info.visible = true
	current_select_equip = new_id
	
	var asa = items_map.get(int(current_select_equip)) as EquipItem
	info_name.text = asa.character_name
	info_content.text = asa.content
	
	
	
	
	
func shopping_button_pressed():
	store.visible = true
	
func _quit_option_inf_button_pressed():
	option_info.visible = false
	
	
func _destroy_equip():
	print("_destroy_equip")
	print(current_select_equip)
	
	items.remove_at(int(current_select_equip))
	items_map.erase(int(current_select_equip))
	
	for i in range(int(current_select_equip)+1,items.size()+1):
		var aa = items_map[i]
		items_map.erase(i)
		items_map[i-1] = aa
	
	
	
	#重新绘制背包
	reload_container()
	refresh_package()
	option_info.visible = false

	
	
	
func reload_container():
	refresh_container(h_box_container_1)
	h_box_container_1.add_child(pakage_block_scene.instantiate())
	h_box_container_1.add_child(pakage_block_scene.instantiate())
	refresh_container(h_box_container_2)
	h_box_container_2.add_child(pakage_block_scene.instantiate())
	h_box_container_2.add_child(pakage_block_scene.instantiate())
	refresh_container(h_box_container_3)
	h_box_container_3.add_child(pakage_block_scene.instantiate())
	h_box_container_3.add_child(pakage_block_scene.instantiate())
	
	
	
	
	
func generate_id(chars,length):
	var word :String
	var n_char = len(chars)
	
	for i in range(length):
		word += chars[randi()%n_char]
	
	return word
